<?php
/**
 * @package    indie-bookstore
 * @subpackage bookstore
 * @author David Neimeyer davidneimeyer@gmail.com
 * administrator/components/com_bookstore/controllers/locator.php
 * @link http://indie-bookstore.googlecode.com/
 * @license    GNU/GPL
 * 
 * @review this was modeled on the weblinks weblink controller;
 * some of the code/methods are probably hanging around; remove 
 * unused code
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

/**
 * bookstore locator Controller
 *
 * @package		indie-bookstore
 * @subpackage	bookstore
 * 
 * @todo dynamically get component name/path; maybe using jroute?
 */
class BookstoreControllerLocator extends BookstoreController
{
	function __construct()
	{
		parent::__construct();

		// Register Extra tasks
		$this->registerTask( 'add', 'edit' );
		//$this->registerTask('maps', 'edit');
	}

	function maps(){
		JRequest::setVar( 'view', 'locator' );
		JRequest::setVar( 'layout', 'maps'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();

		// Checkin the weblink
		$model = $this->getModel('locator');
		//$model->checkout();	
	}
	
	function edit()
	{
		JRequest::setVar( 'view', 'locator' );
		JRequest::setVar( 'layout', 'form'  );
		JRequest::setVar('hidemainmenu', 1);

		parent::display();

		// Checkin the weblink
		//$model = $this->getModel('locator');
		//$model->checkout();
	}

	function save()
	{
		$post	= JRequest::get('post');
		//$cid	= JRequest::getVar( 'cid', array(0), 'post', 'array' );
		//$post['id'] = (int) $cid[0];
		$model = $this->getModel('locator');

		if ($model->store($post)) {
			$msg = JText::_( 'Location Saved' );
		} else {
			foreach($post as $key=>$value){
			$msg .= $key.'='.$value.'&';
			}
			$msg .= JText::_( 'Error Saving Location');
		}

		// Check the table in so it can be edited.... we are done with it anyway
		//$model->checkin();
		$link = 'index.php?option=com_bookstore&view=locator&controller=locator';
		$this->setRedirect($link, $msg);
	}

	function remove()
	{
		global $mainframe;

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('locator');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_bookstore' );
	}


	function publish()
	{
		global $mainframe;

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to publish' ) );
		}

		$model = $this->getModel('locator');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_bookstore' );
	}


	function unpublish()
	{
		global $mainframe;

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to unpublish' ) );
		}

		$model = $this->getModel('locator');
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}

		$this->setRedirect( 'index.php?option=com_bookstore' );
	}

	function cancel()
	{
		// Checkin the weblink
		$model = $this->getModel('locator');
		//$model->checkin();

		$this->setRedirect( 'index.php?option=com_bookstore&controller=locator&view=locator' );
	}


	function orderup()
	{
		$model = $this->getModel('locator');
		$model->move(-1);

		$this->setRedirect( 'index.php?option=com_bookstore');
	}

	function orderdown()
	{
		$model = $this->getModel('locator');
		$model->move(1);

		$this->setRedirect( 'index.php?option=com_bookstore');
	}

	function saveorder()
	{
		$cid 	= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$order 	= JRequest::getVar( 'order', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);
		JArrayHelper::toInteger($order);

		$model = $this->getModel('locator');
		$model->saveorder($cid, $order);

		$msg = 'New ordering saved';
		$this->setRedirect( 'index.php?option=com_bookstore', $msg );
	}
}
?>
